import 'package:device_info/device_info.dart';

import 'package:flutter/material.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import '../../helper/Helper.dart';
import '../../helper/channelHelper.dart';
import '../../helper/dioHelper2.dart';
import '../../helper/routeHelper.dart';
import '../../model/cleancar.dart';

import 'package:get_phone_number/get_phone_number.dart';

import '../../helper/uidata.dart';

import 'cleanobjects_list.dart';

class CleanCarLogin2Page extends StatefulWidget {
  @override
  CleanCarLogin2PageState createState() => new CleanCarLogin2PageState();
}

class CleanCarLogin2PageState extends State<CleanCarLogin2Page> {
  final TextEditingController c1 = new TextEditingController();
  final TextEditingController c2 = new TextEditingController();
  final key = new GlobalKey<ScaffoldState>();
  final DeviceInfoPlugin deviceInfoPlugin = DeviceInfoPlugin();
  late List<CleanCar> carList;
  bool ifLoaded = false;
  late CleanCar carIndex;
  bool ifSended = false;

  final lineBorder = OutlineInputBorder(
    /*边角*/
    borderRadius: BorderRadius.all(
      Radius.circular(8.0), //边角为30
    ),
    borderSide: BorderSide(
      color: Colors.blue, //边线颜色为黄色
      width: 1, //边线宽度为2
    ),
  );

  @override
  void initState() {
    super.initState();
    loadData();
  }

  loadData() async {
    carList = await CleanCar.GetAllList();
    ifLoaded = true;
    getLocal();
    this.setState(() => {});
  }

  getLocal() {
    var xx = Helper.localDB.getString("phone");
    if (xx == null) return;
    carIndex = CleanCar(0, "", 0, "c1.text", xx, "");
    UIData.cleanCar = carIndex;
    RouteHelper.ShowPage(context, CleanObjectsListPage(true));
  }

  List<DropdownMenuItem> generateItemList() {
    List<DropdownMenuItem> items = [];
    for (var a in carList) {
      var item1 = new DropdownMenuItem(
          key: Key(a.CarCode), value: a, child: new Text(a.CarCode));
      items.add(item1);
    }
    return items;
  }

  getContain(Widget c1) {
    return Container(
      width: double.infinity,
      margin: EdgeInsets.only(top: 16.0, left: 16.0, right: 16.0),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.all(Radius.circular(8)),
      ),
      child: c1,
    );
  }

  getButton() {
    return Container(
      padding: EdgeInsets.symmetric(vertical: 0.0, horizontal: 30.0),
      width: double.infinity,
      margin: EdgeInsets.only(top: 12.0),
      child: ElevatedButton(
        child: Container(
            padding: EdgeInsets.only(top: 12.0, bottom: 12.0),
            child: Text(
              "登录",
              style: TextStyle(color: Colors.white, fontSize: 16.0),
            )),
        onPressed: () async {
          //var aInfo=await deviceInfoPlugin.androidInfo;
          var pb1 = await DioHelper.Get(
              "/User/CheckYzm?phone=" + c1.text + "&yzm=" + c2.text, {});
          print(pb1);
          if (pb1.toString() == "false") {
            ChannelHelper.showToast("验证码错误！");
            return;
          }
          Helper.localDB.setString("phone", c1.text);
          carIndex = CleanCar(0, "", 0, "c1.text", c1.text, "");
          UIData.cleanCar = carIndex;
          RouteHelper.ShowPage(context, CleanObjectsListPage(true));
        },
      ),
    );
  }

  getButton2() {
    return TextButton(
      child: Container(
          padding: EdgeInsets.only(top: 12.0, bottom: 12.0),
          child: Text(
            "获取验证码",
            style: TextStyle(color: Colors.white, fontSize: 16.0),
          )),
      onPressed: () async {
        if (!ifSended) {
          var pb1 = Helper.isChinaPhoneLegal(c1.text);
          if (!pb1) {
            ChannelHelper.showToast("手机号码有误！");
            return;
          }
          DioHelper.Get("/User/GetYzm?phone=" + c1.text, {});
          ifSended = true;
          ChannelHelper.showToast("已发送，注意查收");
        }
      },
    );
  }

  getHeader() {
    return Container(
      margin: EdgeInsets.only(bottom: 48.0),
      child: Text(
        UIData.appName,
        style: TextStyle(
            fontWeight: FontWeight.w400, color: Colors.white, fontSize: 24.0),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        //backgroundColor: Colors.white,
        key: key,
        body: ifLoaded == false
            ? Center(
                child: SpinKitCircle(
                color: Colors.blue,
              ))
            : Container(
                decoration: BoxDecoration(
                  image: DecorationImage(
                    image: AssetImage("lib/assets/images/bg.png"),
                    fit: BoxFit.cover,
                  ),
                  color: Colors.transparent,
                ),
                child: Center(
                  child: loginFields(context),
                ),
              ));
  }

  loginBody(BuildContext context) => SingleChildScrollView(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: <Widget>[loginFields(context)],
        ),
      );

  Widget getInput2(String title, TextEditingController c1, {double w1 = 120}) {
    return ListTile(
        title: Text(
          title,
          // style: TextStyle(fontWeight: FontWeight.bold),
        ),
        trailing: Container(
            width: w1,
            child: TextField(
              maxLines: 1,
              controller: c1,
              textAlign: TextAlign.center,
            )));
  }

  loginFields(BuildContext context) => SingleChildScrollView(
        child: Container(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              getHeader(),
              SizedBox(
                height: 20.0,
              ),
              // getContain(ListTile(title: getInput2("电话号码", c1),trailing: ElevatedButton(child:Text("验证码"),onPressed: (){
              //     DioHelper.Get("/User/GetYzm?phone="+c1.text, {});
              // },),)),
              getContain(getInput2("手机号码", c1)),
              SizedBox(
                height: 20.0,
              ),
              getContain(getInput2("短信验证码", c2)),
              SizedBox(
                height: 30.0,
              ),
              getButton(),
              SizedBox(
                height: 10.0,
              ),
              getButton2(),
              SizedBox(
                height: 20.0,
              ),
              //ElevatedButton(child:Text("管理员登录",style: TextStyle(color:Colors.grey),),onPressed: ()=>RouteHelper.ShowPage(context, LoginPage()),)
            ],
          ),
          margin: EdgeInsets.all(12.0),
        ),
      );
}
